package com.salt.core.log.utils;

import com.salt.common.utils.SpringContextHolder;
import com.salt.core.log.prop.KafkaProperties;
import com.salt.core.log.sender.KafkaSender;
import com.salt.modules.log.domain.LoginLog;
import org.apache.commons.lang3.time.DateFormatUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class LoginRecordUtils {

	private static KafkaSender kafkaSender = SpringContextHolder.getBean(KafkaSender.class);
	private static KafkaProperties kafkaProperties = SpringContextHolder.getBean(KafkaProperties.class);

	private static final String LOGIN_LOG_TOPIC = "login-log";

	/**
	 * 发送日志
	 */
	public static void senderLog(HttpServletRequest request,
							   String userid, String realname, String userName, String status,
							   String logType, String msg){

		if (!kafkaProperties.getOperEnable())
			return;

		LoginLog loginLog = new LoginLog();
		loginLog.setOperUserId(userid);
		loginLog.setOperRealName(realname);
		loginLog.setUserName(userName);
		loginLog.setStatus(status);
		loginLog.setMsg(msg);
		//loginLog.setLoginMethod(loginMethod);
		//loginLog.setAccessTime(DateUtil.getDateTime());
		loginLog.setAccessTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		loginLog.setIp(LogUtil.getIpAddress(request));
		loginLog.setUrl(request.getRequestURI());
		loginLog.setLogType(logType);

		kafkaSender.send(LOGIN_LOG_TOPIC, loginLog);

		// 异步发送日志
		//new SenderLogThread(loginLog).start();
	}

	/**
	 * 发送登录日志线程
	 */
	/*public static class SenderLogThread extends Thread{

		private LoginLog loginLog;

		public SenderLogThread(LoginLog loginLog){
			super(SenderLogThread.class.getSimpleName());
			this.loginLog = loginLog;
		}

		@Override
		public void run() {
			kafkaSender.send(loginLog);
		}
	}*/




}
